package org.my.workflow.element;

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.Collections;
import java.util.Collection;

import org.w3c.dom.Element;

/**
 * @author <a href="mailto:yang_y@sysnet.com.cn">Young Yang</a>
 */
public class WorkflowElement implements ConfigElement {

    private String name;
    private InitialActionElement initialActionElement;

    private Map<String, StepElement> stepElements = new HashMap<String, StepElement>();

    public void processXML(Element element) {
        name = XMLUtils.getAtrributeValue(element,"name");

        initialActionElement = new InitialActionElement();
        initialActionElement.processXML(XMLUtils.getChildElementByTagName(element, "initial-action"));

        List<Element> stepXMLElements = XMLUtils.getElementsByTagName(element,"step");
        for(Element stepXMLElement : stepXMLElements){
            StepElement stepElement = new StepElement();
            stepElement.processXML(stepXMLElement);
            stepElements.put(stepElement.getId(), stepElement);
        }
    }

    public String getName() {
        return name;
    }

    public InitialActionElement getInitialActionElement() {
        return initialActionElement;
    }

    public StepElement getStepElementById(String stepId){
        return stepElements.get(stepId);
    }

    public Collection<StepElement> getStepElements(){
        return Collections.unmodifiableCollection(stepElements.values());
    }

    public static void main(String[] args) {

    }
}
